﻿using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.VisualStudio.Shell;


namespace Lexware.Tools.ProjectPropertyOverview
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [Guid("8AA9FE35-03FA-4A09-81F5-D4DCB5810C10")]

    public class ToolOptions : DialogPage

    {

        # region Fields

        private string _platformTarget = "x86";
        private bool _runCodeAnalysis = true;
        private string _warningLevel = "4";
        private bool _treatWarningsAsErrors = true;
        
        private bool _isVisible;

        # endregion

        # region Public

        public string PlatformTarget
        {
            get { return _platformTarget; }
            set { _platformTarget = value; }
        }

        public string WarningLevel
        {
            get { return _warningLevel; }
            set { _warningLevel = value; }
        }

        public bool RunCodeAnalysis
        {
            get { return _runCodeAnalysis; }
            set { _runCodeAnalysis = value; }
        }

        public bool TreatWarningsAsErrors
        {
            get { return _treatWarningsAsErrors; }
            set { _treatWarningsAsErrors = value; }
        }

        # endregion



        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope"), Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected override IWin32Window Window
        {
            get
            {
                OptionsControl page = new OptionsControl();
                page.optionsPage = this;
                page.Initialize();
                return page;
            }
        }

        protected override void OnActivate(CancelEventArgs e)
        {
            DTE dte = (DTE)GetService(typeof(DTE));
            _isVisible = (dte.Windows.Item("{a65cbe7a-934a-428c-8065-38fad7a9a8df}").Visible);
            base.OnActivate(e);
        }

        // deactivate and reactivate the tool window to update error icons
        protected override void OnApply(PageApplyEventArgs e)
        {
            base.OnApply(e);
            if (_isVisible)
            {
                DTE dte = (DTE) GetService(typeof (DTE));
                dte.Windows.Item(Constants.vsWindowKindSolutionExplorer).SetFocus();
                dte.Windows.Item("{a65cbe7a-934a-428c-8065-38fad7a9a8df}").SetFocus();
            }
        }

    }
}
